//
//  JWNewGraduatCtry.m
//  test
//
//  Created by 李彦宏 on 2018/3/7.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "JWNewGraduatCtry.h"

#import "JWNGraduatehead.h"//search头
#import "JWGRNewTabcell.h"//Seacher cell

//#import "GraduatNewcell.h"//研究生cell
#import "JWGRUsermodel.h"//研究生信息model
#import "JWUserManager.h"//管理类
#import "YHGradRegistraVC.h" //研究生注册界面
#import "MLSearchViewController.h"//搜索界面
#import "JWGRChankanVC.h"//查看详情界面


static NSString *const GraduatNewcellID = @"HomePlanClassNameCell";
@interface JWNewGraduatCtry ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) JWGRUsermodel *model;
@property (nonatomic, strong) NSMutableArray *GRarrylist;

@property(nonatomic,assign)NSInteger pageNum;

@end

@implementation JWNewGraduatCtry

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   [self.view addSubview:self.tableView];
    self.pageNum = 1;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageNum = 1;
        [self setGRdate];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageNum ++;
        [self setGRdate];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setGRdate];
}
#pragma -mark ————————————————研究生数据————————————————————
-(void)setGRdate{
   
   __weak typeof(self) weakslf = self;
 
    NSDictionary *dict=@{
                         @"jwid":JWid,
                         @"jwpassword":JWpassword,
                         @"page" : @(self.pageNum),
                         };
//    NSLog(@">a>%ld", (long)self.pageNum);
  //  NSLog(@"--dict--%@", dict);
    //    参数  分页=>page 教务id=〉jwid ，秘码=>jwpassword
    [[AFNetworkTool shareManager]getJSONWithUrl:JWteacher_ChangeGR parame:dict successData:^(id json) {
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        if (self.pageNum == 1) {
         [self.GRarrylist removeAllObjects];
        }

        NSArray * arr = json[@"data"];
        
        if (arr.count == 0) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
       
        for(int i=0;i<arr.count;i++){
            
            self.model = [JWGRUsermodel yy_modelWithJSON:arr[i]];
            [JWUserManager saveGR:self.model];
            
            [weakslf.GRarrylist addObject: self.model ];
           
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakslf.tableView reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
        [Utility showFail:@"网络连接错误..."];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];
    
}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.GRarrylist.count;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JWGRNewTabcell *cell=[JWGRNewTabcell cellwithTableview:tableView];
    
    cell.model = self.GRarrylist[indexPath.row];
    return cell;
    
}
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 44;
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.model=self.GRarrylist[indexPath.row];
    
    [JWUserManager saveGR:self.model];
    JWGRChankanVC *vc=[[JWGRChankanVC alloc]init];
    vc.title=[NSString stringWithFormat:@"%@的详情",self.model.name];
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 70;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

#pragma -mark 头部的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    //添加研究生
    JWNGraduatehead *head1=[[JWNGraduatehead alloc]init];
    [head1.AddGreduBtn addTarget:self action:@selector(AddButActi) forControlEvents:UIControlEventTouchUpInside];
    //搜索
    [head1.ShearBtn addTarget:self action:@selector(searchBarAction:) forControlEvents:UIControlEventTouchUpInside];
    head1.ShearBtn.layer.cornerRadius=5;
    [head1.ShearBtn setTitleEdgeInsets:UIEdgeInsetsMake(0,-50, 0,10)];
    [head1.ShearBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 90, 0, -60)];
    
    
    return head1;
    
    
}
#pragma -mark ————————————搜索按钮响应————————————————————
-(void)searchBarAction:(UIButton *)send{
    MLSearchViewController *vc=[[MLSearchViewController alloc]init];
    vc.tagsArray=[[NSMutableArray alloc]init];
    
    if(_GRarrylist.count<7){
        vc.tagsArray=[NSMutableArray arrayWithObjects:@"李彦宏",@"朱圳",@"严戈闲",@"赵延",@"田涛",@"肖欢欣",@"游鹤亭",@"陈炽", nil];    }else{
        for (int i=0; i<7; i++) {
            self.model =_GRarrylist[i];
            [vc.tagsArray addObject:self.model.name];
        }
    }
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma -mark —————————————添加研究生————————————————————
-(void)AddButActi{
    NSLog(@"添加研究生");
    YHGradRegistraVC *vc1=[[YHGradRegistraVC alloc]init];
    vc1.title=@"研究生注册";
    [self.navigationController pushViewController:vc1 animated:YES];
    
}

#pragma -mark ———————————————————懒加载————————————————————

- (NSMutableArray *)GRarrylist {
    
    if(!_GRarrylist){
        _GRarrylist = [NSMutableArray array];
    }
    return _GRarrylist;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView=[[UIView alloc]init];
        _tableView.delegate = self;
        _tableView.rowHeight=45;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
